home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / tutorials / custEducation / opengl2 / examples / nurbs / surface.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  4.6 KB  |  209 lines

  1. /*
  2.  * Copyright 1993, 1995, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17.  
  18. /* surface.c
  19.  * This program draws a rotating NURBS surface.
  20.  *
  21.  *    Escape key    - exit the program
  22.  */
  23. #include <GL/gl.h>
  24. #include <GL/glu.h>
  25. #include <GL/glut.h>
  26.  
  27. #include <math.h>
  28. #include <stdio.h>
  29.  
  30. /*  Function Prototypes  */
  31.  
  32. GLvoid  initgfx( GLvoid );
  33. GLvoid  drawScene( GLvoid );
  34. GLvoid  reshape( GLsizei, GLsizei );
  35. GLvoid  animate( GLvoid );
  36. GLvoid  visibility( GLint );
  37. GLvoid  keyboard( GLubyte, GLint, GLint );
  38.  
  39. GLvoid initSurface( GLvoid );
  40.  
  41. void printHelp( char * );
  42.  
  43. /* Global Definitions */
  44.  
  45. #define KEY_ESC    27    /* ascii value for the escape key */
  46.  
  47. /* Global Variables */
  48.  
  49. static GLfloat ctlpoints[4][4][3];
  50. static GLUnurbsObj *theNurb;
  51.  
  52. static GLfloat angle = 0.0;
  53.  
  54. void
  55. main(int argc, char *argv[])
  56. {
  57.     GLsizei     width, height;
  58.  
  59.     glutInit( &argc, argv );
  60.  
  61.     width = glutGet( GLUT_SCREEN_WIDTH ); 
  62.     height = glutGet( GLUT_SCREEN_HEIGHT );
  63.     glutInitWindowPosition( width/4, height/4 ); 
  64.     glutInitWindowSize( width/2, height/2 );
  65.     glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE );
  66.     glutCreateWindow( argv[0] );
  67.     
  68.     initgfx();
  69.  
  70.     glutKeyboardFunc( keyboard );
  71.     glutReshapeFunc( reshape );
  72.     glutIdleFunc( animate ); 
  73.     glutVisibilityFunc( visibility ); 
  74.     glutDisplayFunc( drawScene ); 
  75.  
  76.     printHelp( argv[0] );
  77.  
  78.     glutMainLoop();
  79. }
  80.  
  81. void
  82. printHelp( char *progname )
  83. {
  84.     fprintf(stdout, "\n%s - draw a rotating NURBS surface\n"
  85.         "Escape key        - exit the program\n\n",
  86.         progname);
  87. }
  88.  
  89. GLvoid
  90. initgfx( GLvoid )
  91. {
  92.     GLfloat mat_diffuse[] = { 0.8, 0.8, 0.2, 1.0 };
  93.     GLfloat mat_specular[] = { 0.8, 0.0, 0.8, 1.0 };
  94.     GLfloat mat_emission[] = { 0.0, 0.0, 0.0, 1.0 };
  95.     GLfloat mat_shininess[] = { 30.0 };
  96.  
  97.     GLfloat light_position[] = { 1.0, 0.0, 1.0, 0.0 };
  98.  
  99.     glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, mat_diffuse);
  100.     glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular);
  101.     glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, mat_emission);
  102.     glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, mat_shininess);
  103.     glLightModelf(GL_LIGHT_MODEL_TWO_SIDE, 1.0);
  104.     glLightfv(GL_LIGHT0, GL_POSITION, light_position);
  105.  
  106.     glEnable(GL_LIGHTING);
  107.     glEnable(GL_LIGHT0);
  108.  
  109.     glClearColor( 0, 0, 0, 1 );
  110.     glEnable( GL_DEPTH_TEST );
  111.  
  112.     /* automatically generate normals for the nurbs surface */
  113.     glEnable( GL_AUTO_NORMAL );
  114.  
  115.     initSurface();
  116.  
  117.     theNurb = gluNewNurbsRenderer();
  118. }
  119.  
  120. /*  Initializes the control points of the surface to a small hill.
  121.  *  The control points range from -3 to +3 in x, y, and z
  122.  */
  123. GLvoid initSurface(void)
  124. {
  125.     int s, t;
  126.  
  127.     for (s = 0; s < 4; s++) {
  128.         for (t = 0; t < 4; t++) {
  129.             ctlpoints[s][t][0] = 2.0*((GLfloat)s - 1.5);
  130.             ctlpoints[s][t][1] = 2.0*((GLfloat)t - 1.5);
  131.  
  132.             if ( (s == 1 || s == 2) && (t == 1 || t == 2))
  133.                 ctlpoints[s][t][2] = 3.0;
  134.             else
  135.                 ctlpoints[s][t][2] = -3.0;
  136.         }
  137.     }
  138. }
  139.  
  140. GLvoid
  141. reshape( GLsizei width, GLsizei height )
  142. {
  143.     GLdouble    aspect;
  144.  
  145.     glViewport( 0, 0, width, height );
  146.  
  147.     aspect = (GLdouble) width / (GLdouble) height;
  148.  
  149.     glMatrixMode( GL_PROJECTION );
  150.     glLoadIdentity();
  151.     gluPerspective( 45.0, aspect, 3.0, 20.0 );
  152.     glMatrixMode( GL_MODELVIEW );
  153.     glLoadIdentity();
  154.     glTranslatef( 0.0, 0.0, -15.0 ); 
  155. }
  156.  
  157. GLvoid 
  158. keyboard( GLubyte key, GLint x, GLint y )
  159. {
  160.     switch (key) {
  161.     case KEY_ESC:        /* Exit when the Escape key is pressed */
  162.         exit(0);
  163.     }
  164. }
  165.  
  166. GLvoid
  167. animate( GLvoid )
  168. {
  169.     /* update the current angle */
  170.     angle = fmodf( (angle + 5.0), 360.0 );
  171.  
  172.     glutPostRedisplay();    /* Tell GLUT to redraw */
  173. }
  174.         
  175. GLvoid
  176. visibility( int state )
  177. {
  178.     if (state == GLUT_VISIBLE) {
  179.         glutIdleFunc( animate );
  180.     } else {
  181.         glutIdleFunc( NULL );
  182.     }
  183. }
  184.  
  185. GLvoid
  186. drawScene( GLvoid )
  187. {
  188.     GLfloat knots[8] = {0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0};
  189.  
  190.     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  191.     glPushMatrix(); 
  192.     
  193.         glRotatef( angle, 0.0, 1.0, 0.0 );
  194.         gluBeginSurface( theNurb );
  195.             gluNurbsSurface( theNurb, 
  196.                 8, knots,
  197.                 8, knots,
  198.                 4 * 3, 3,
  199.                 &ctlpoints[0][0][0], 
  200.                 4, 4,
  201.                 GL_MAP2_VERTEX_3 );
  202.         gluEndSurface( theNurb );
  203.  
  204.     glPopMatrix();
  205.  
  206.     glutSwapBuffers();
  207. }
  208.  
  209.